import React, { PureComponent } from 'react'
import { Modal, Form, Input, Upload, Button, message } from 'antd';
import { addPolicy } from '@/services/notice'
export default class AddModal extends PureComponent<any, any> {
    constructor(props: any) {
        super(props)
        this.state = {
            treeData: [],
            doc: [],
            cover: [],
            sort: '0',
            name: '',
            loading:false
        }
    }
    changeInputValue = (e: any, name: string) => {
        this.setState({
            [name]: e.target.value
        })
    }
    changeFile = (info: any, name: string) => {
        let fileList = [...info.fileList];
        fileList = fileList.slice(-1);
        fileList = fileList.map(file => {
            if (file.response) {
                file.url = file.response.url;
            }
            return file;
        });
        this.setState({ [name]: fileList });
    };
    submitHanle = () => {
        const { doc, cover, sort, name } = this.state;
        if(!doc[0]||!cover[0]||!sort||!name)return message.error('请完善政策法规信息')
        this.setState({
            loading:true
        })
        addPolicy({
            sort,name,
            cover: cover[0].originFileObj,
            doc: doc[0].originFileObj
        }).then(res=>{
            if(res.status===1){
                message.success('添加成功')
                const {hideHandle,getList,currentPage} = this.props;
                hideHandle()
                getList(currentPage);
                this.setState({
                    loading:false
                })
            }else{
                message.error('添加失败')
            }
        })
    }
    render() {
        const {  visible, hideHandle } = this.props;
        const { doc, cover, sort, name ,loading} = this.state;
        return (
            <Modal visible={visible} title="添加政策法规" confirmLoading={loading} onCancel={hideHandle} onOk={this.submitHanle}>
                <Form>
                    <Form.Item label="政策法规标题" required><Input onChange={(e: any) => this.changeInputValue(e, 'name')} value={name} /></Form.Item>
                </Form>
                <Form>
                    <Form.Item label="上传文件" required>
                        <Upload beforeUpload={()=>{return false}} accept=".pdf,.mp4,.ppt" onChange={(e) => this.changeFile(e, 'doc')} fileList={doc}>
                            <Button>上传文件</Button>
                        </Upload>
                    </Form.Item>
                </Form>
                <Form>
                    <Form.Item label="上传封面" required >
                        <Upload listType='picture' beforeUpload={()=>{return false}} accept=".jpg,.png,.ppt" onChange={(e) => this.changeFile(e, 'cover')} fileList={cover}>
                            <Button>上传封面</Button>
                        </Upload>
                    </Form.Item>
                </Form>
                <Form>
                    <Form.Item label="排列序号" required><Input onChange={(e: any) => this.changeInputValue(e, 'sort')} value={sort} /></Form.Item>
                </Form>
            </Modal>
        )
    }
}
